Handling date with R package:[lubridate]

package for date manipulation

packages
Author

Tony Duan

Published

August 11, 2023

1 date format

Code
library(tidyverse)
library(lubridate)
library(nycflights13)

input as character

Code
date1='2023-01-01'
class(date1)
[1] "character"

convert into date type with as.Date()

Code
date2=as.Date('2023-01-01')
class(date2)
[1] "Date"

convert into date type with ymd()

Code
date3=ymd('2023-01-01')
class(date3)
[1] "Date"

get today with today()

Code
today()
[1] "2023-08-30"

get local timezone

Code
Sys.timezone()
[1] "Asia/Shanghai"

2 change date format

make multiple column character to date with make_date()

Code
flights %>% 
  select(year, month, day, hour, minute) %>% 
  mutate(departure = make_date(year, month, day))
# A tibble: 336,776 × 6
    year month   day  hour minute departure 
   <int> <int> <int> <dbl>  <dbl> <date>    
 1  2013     1     1     5     15 2013-01-01
 2  2013     1     1     5     29 2013-01-01
 3  2013     1     1     5     40 2013-01-01
 4  2013     1     1     5     45 2013-01-01
 5  2013     1     1     6      0 2013-01-01
 6  2013     1     1     5     58 2013-01-01
 7  2013     1     1     6      0 2013-01-01
 8  2013     1     1     6      0 2013-01-01
 9  2013     1     1     6      0 2013-01-01
10  2013     1     1     6      0 2013-01-01
# ℹ 336,766 more rows
Code
flights %>% 
  select(year, month, day, hour, minute) %>% 
  mutate(departure = make_datetime(year, month, day, hour, minute))
# A tibble: 336,776 × 6
    year month   day  hour minute departure          
   <int> <int> <int> <dbl>  <dbl> <dttm>             
 1  2013     1     1     5     15 2013-01-01 05:15:00
 2  2013     1     1     5     29 2013-01-01 05:29:00
 3  2013     1     1     5     40 2013-01-01 05:40:00
 4  2013     1     1     5     45 2013-01-01 05:45:00
 5  2013     1     1     6      0 2013-01-01 06:00:00
 6  2013     1     1     5     58 2013-01-01 05:58:00
 7  2013     1     1     6      0 2013-01-01 06:00:00
 8  2013     1     1     6      0 2013-01-01 06:00:00
 9  2013     1     1     6      0 2013-01-01 06:00:00
10  2013     1     1     6      0 2013-01-01 06:00:00
# ℹ 336,766 more rows

3 day differnce between two dates

Code
day1=ymd('2022-01-01')
day2=ymd('2023-02-03')

diff=day2-day1
Code
diff
Time difference of 398 days

using interval() find two dates gap

Code
interval(day1,day2) %>% as.period()
[1] "1y 1m 2d 0H 0M 0S"

find day gap

Code
interval(day1,day2)%/% days(1)
[1] 398

find month gap

Code
interval(day1,day2)%/% months(1)
[1] 13

find year gap

Code
interval(day1,day2)%/% years(1)
[1] 1

4 day and time

Code
now_time=now()
now_time
[1] "2023-08-30 18:44:38 CST"

get year

Code
year(now_time)
[1] 2023

get month

Code
month(now_time)
[1] 8

get date of the month

Code
mday(now_time)
[1] 30

get date of the year

Code
yday(now_time)
[1] 242

get date of the week

Code
wday(now_time)
[1] 4

get hour

Code
hour(now_time)
[1] 18

get minute

Code
minute(now_time)
[1] 44

get second

Code
second(now_time)
[1] 38.08057

5 Reference

https://r4ds.had.co.nz/dates-and-times.html